package swordoffer.offer52;

import java.util.HashSet;

public class HashSolution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        HashSet<ListNode> nodes = new HashSet<>();
        while(pHead1 != null){
            nodes.add(pHead1);
            pHead1 = pHead1.next;
        }
        while (pHead2 != null){
            if(nodes.contains(pHead2)){
                return pHead2;
            }
            pHead2 = pHead2.next;
        }
        return null;
    }
}
